package com.jin.webvideo.config.filter.access.handler;

import com.jin.webvideo.config.filter.access.AccessControl;
import com.jin.webvideo.config.filter.access.GlobalStore;
import com.jin.webvideo.config.filter.access.rule.ForbidHandler;
import com.jin.webvideo.entity.SessionStore;
import com.jin.webvideo.util.SimpleTemplateParser;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义的访问处理
 */
@Data
public class SimpleAccessHandler implements AccessHandler {
    protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleAccessHandler.class);
    private String information = "服务器内部错误，请联系相关人员提供支持";
    private String name = "SimpleAccessHandler";
    private int order = Integer.MAX_VALUE;
    private int httpCode = 601;
    private AccessRule accessRule = AccessRule.NONE;
    private ForbidHandler forbidHandler = ForbidHandler.NONE;

    public SimpleAccessHandler(String name, int order) {
        this.name = name;
        this.order = order;
    }
    public SimpleAccessHandler() {}
    @Override
    public boolean handle(GlobalStore globalStore, SessionStore store, HttpServletRequest request, AccessControl.Access access, HandlerChain handlerChain) {
        if (accessRule.compute(globalStore, store, request)) {
            LOGGER.info("禁止访问 禁止规则[{}] 后处理规则[{}]", name, forbidHandler.name());
            //String msg = parser.getValue(information, String.class);
            AccessControl.Access.forbid(access, httpCode, information);
            forbidHandler.handle(globalStore, store, request);
            return true;
        }
        return handlerChain.doHandle(access, globalStore, request, store);
    }

    @Override
    public int order() {
        return order;
    }
}
